<?php

/**
 * DadosUsuario form base class.
 *
 * @package    form
 * @subpackage dados_usuario
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 8508 2008-04-17 17:39:15Z fabien $
 */
class BaseDadosUsuarioForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id_dados_usuario'    => new sfWidgetFormInputHidden(),
      'id_usuario'          => new sfWidgetFormDoctrineChoice(array('model' => 'sfGuardUser', 'add_empty' => true)),
      'id_usuario_pai'      => new sfWidgetFormDoctrineChoice(array('model' => 'DadosUsuario', 'add_empty' => true)),
      'tp_usuario'          => new sfWidgetFormInput(),
      'no_usuario'          => new sfWidgetFormInput(),
      'nu_cpf'              => new sfWidgetFormInput(),
      'nu_cpf_pai'          => new sfWidgetFormInput(),
      'nu_cpf_mae'          => new sfWidgetFormInput(),
      'no_usuario_pai'      => new sfWidgetFormInput(),
      'no_usuario_mae'      => new sfWidgetFormInput(),
      'sobrenome'           => new sfWidgetFormInput(),
      'email'               => new sfWidgetFormInput(),
      'nu_ddd_tel'          => new sfWidgetFormInput(),
      'nu_tel'              => new sfWidgetFormInput(),
      'st_estado_civil'     => new sfWidgetFormInput(),
      'nu_renda'            => new sfWidgetFormInput(),
      'nu_tempo_residencia' => new sfWidgetFormInput(),
      'ds_profissao'        => new sfWidgetFormInput(),
      'sexo'                => new sfWidgetFormInput(),
      'dt_nascimento'       => new sfWidgetFormDateTime(),
      'groups_list'         => new sfWidgetFormDoctrineChoiceMany(array('model' => 'sfGuardGroup')),
      'permissions_list'    => new sfWidgetFormDoctrineChoiceMany(array('model' => 'sfGuardPermission')),
    ));

    $this->setValidators(array(
      'id_dados_usuario'    => new sfValidatorDoctrineChoice(array('model' => 'DadosUsuario', 'column' => 'id_dados_usuario', 'required' => false)),
      'id_usuario'          => new sfValidatorDoctrineChoice(array('model' => 'sfGuardUser', 'required' => false)),
      'id_usuario_pai'      => new sfValidatorDoctrineChoice(array('model' => 'DadosUsuario', 'required' => false)),
      'tp_usuario'          => new sfValidatorString(array('max_length' => 1, 'required' => false)),
      'no_usuario'          => new sfValidatorString(array('max_length' => 50)),
      'nu_cpf'              => new sfValidatorString(array('max_length' => 14, 'required' => false)),
      'nu_cpf_pai'          => new sfValidatorString(array('max_length' => 14, 'required' => false)),
      'nu_cpf_mae'          => new sfValidatorString(array('max_length' => 14, 'required' => false)),
      'no_usuario_pai'      => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'no_usuario_mae'      => new sfValidatorString(array('max_length' => 50, 'required' => false)),
      'sobrenome'           => new sfValidatorString(array('max_length' => 150, 'required' => false)),
      'email'               => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'nu_ddd_tel'          => new sfValidatorInteger(array('required' => false)),
      'nu_tel'              => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'st_estado_civil'     => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'nu_renda'            => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'nu_tempo_residencia' => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'ds_profissao'        => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'sexo'                => new sfValidatorString(array('max_length' => 1, 'required' => false)),
      'dt_nascimento'       => new sfValidatorDateTime(array('required' => false)),
      'groups_list'         => new sfValidatorDoctrineChoiceMany(array('model' => 'sfGuardGroup', 'required' => false)),
      'permissions_list'    => new sfValidatorDoctrineChoiceMany(array('model' => 'sfGuardPermission', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('dados_usuario[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'DadosUsuario';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['groups_list']))
    {
      $this->setDefault('groups_list', $this->object->groups->getPrimaryKeys());
    }

    if (isset($this->widgetSchema['permissions_list']))
    {
      $this->setDefault('permissions_list', $this->object->permissions->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->savegroupsList($con);
    $this->savepermissionsList($con);
  }

  public function savegroupsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['groups_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->groups->getPrimaryKeys();
    $values = $this->getValue('groups_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('groups', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('groups', array_values($link));
    }
  }

  public function savepermissionsList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['permissions_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->permissions->getPrimaryKeys();
    $values = $this->getValue('permissions_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('permissions', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('permissions', array_values($link));
    }
  }

}
